package com.example.webdemo.service.impl;

import com.example.webdemo.dao.ArticleInfoMapper;
import com.example.webdemo.dao.ArticleMapper;
import com.example.webdemo.dao.ImageRecordMapper;
import com.example.webdemo.model.Article;
import com.example.webdemo.model.ArticleInfo;
import com.example.webdemo.model.ImageRecord;
import com.example.webdemo.model.Tag;
import com.example.webdemo.service.ArticleInfoService;
import com.example.webdemo.service.ArticleService;
import com.example.webdemo.service.ImageRecordService;
import com.example.webdemo.service.TagsService;
import com.example.webdemo.utils.MyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zp
 * @version 1.0
 * @description: ArticleServiceImpl
 * @date 2023/7/31 19:13
 */
@Service
public class ArticleInfoServiceImpl implements ArticleInfoService {
    @Resource
    private ArticleInfoMapper articleInfoMapper;
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private TagsService tagsService;
    @Resource
    private ImageRecordMapper imageRecordMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return articleInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ArticleInfo record) {
        return articleInfoMapper.insert(record);
    }

    @Override
    public int insertSelective(ArticleInfo record) {
        return articleInfoMapper.insertSelective(record);
    }

    @Override
    public ArticleInfo selectByPrimaryKey(Integer id) {
        return articleInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ArticleInfo record) {
        return articleInfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ArticleInfo record) {
        return articleInfoMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<ArticleInfo> selectAll() {
        return articleInfoMapper.selectAll();
    }

    @Override
    public List<ArticleInfo> selectByUserId(Integer userId) {
        List<ArticleInfo> articles = articleInfoMapper.selectByUserId(userId);
        if (articles == null) {
            return new ArrayList<>();
        }
        List<Tag> tags = tagsService.selectAll();
        for (ArticleInfo article : articles) {
            List<Tag> tagList = new ArrayList<>();
            String[] tagIds = article.getTags().split(",");
            for (String id : tagIds) {
                Integer tagId = Integer.parseInt(id);
                for (Tag tag : tags) {
                    if (tagId.equals(tag.getId())) {
                        tagList.add(tag);
                    }
                }
            }
            article.setTagNames(tagList);
        }
        return articles;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveArticleInfoAndArticleTitle(ArticleInfo articleInfo) {
        List<Integer> tagIds = articleInfo.getTagNames().stream().map(Tag::getId).collect(Collectors.toList());
        StringBuilder tagsStr = new StringBuilder();
        Article article = new Article();
        article.setSpecialId(tagIds.get(0));
        for (Integer tagId : tagIds) {
            tagsStr.append(tagId).append(",");
        }
        String tagList = tagsStr.substring(0, tagsStr.length() - 1);
        articleInfo.setTags(tagList);
        BeanUtils.copyProperties(articleInfo, article);

        ImageRecord imageRecord = imageRecordMapper.selectByImageUrlAndUser(MyUtils.getImageNameByPath(articleInfo.getThumb()), articleInfo.getUserId());
        if(imageRecord == null){
            return false;
        }
        imageRecord.setImageType(1);
        int imageRecordRow = imageRecordMapper.updateByPrimaryKeySelective(imageRecord);
        if (imageRecordRow <= 0) {
            return false;
        }

        int articleInfoId = articleInfoMapper.insertSelective(articleInfo);
        if (articleInfoId <= 0) {
            return false;
        }
        article.setArticleInfoId(articleInfoId);
        int articleId = articleMapper.insertSelective(article);
        return articleId > 0;
    }
}
